import React, {PureComponent} from "react";
import {Divider, notification, Table} from "antd";
import {tableProps} from "../../lib/ui";
import {lockFeeById, showCreateOrderFee, showUpdateOrderOutFee, unlockFeeById, updateOrderOutFee} from "./actions";
import {connect} from "react-redux";
import UpdateOrderOutFeeModal from "./UpdateOrderOutFeeModal";
import {getPrincipal} from "../../lib/identity";

class OrderOutFeeModal extends PureComponent {

    handleShowCreate = () => {
        const {dispatch} = this.props;
        dispatch(showCreateOrderFee());
    };
    handleUpdate = record => {
        const {dispatch} = this.props;
        dispatch(showUpdateOrderOutFee(record));
    };

    handleLock = record => {
        const {dispatch, fees} = this.props;
        dispatch(lockFeeById(record.id)).then(action => {
            if (action.error !== true) {
                fees.forEach(item => {
                    if (item.id === record.id) {
                        item.status = "LOCK";
                    }
                });
                dispatch(updateOrderOutFee(fees));
            }
        });
    };

    handleUnLock = record => {
        const {dispatch, fees} = this.props;
        dispatch(unlockFeeById(record.id)).then(action => {
            if (action.error !== true) {
                fees.forEach(item => {
                    if (item.id === record.id) {
                        item.status = "UNLOCK";
                    }
                });
                dispatch(updateOrderOutFee(fees));
            }
        });
    };

    handleDelete = id => {
        const {dispatch, fees} = this.props;
        let deleteIndex = fees.findIndex(item => item.id === id);
        fees.splice(deleteIndex, 1);
        dispatch(updateOrderOutFee(fees));
        notification.success({
            message: '移除成功'
        });
    };

    render() {
        const {fees, loading} = this.props;
        let dataSource = [];
        fees.forEach(fee => {
            if (fee.carrierId !== null && fee.carrierId.length > 0 && "OUT" === fee.type) {
                dataSource.push({...fee});
            }
        });
        const columns = [
            {
                title: "费用科目",
                dataIndex: "feeName",
                width: "150px",
            }, {
                title: "金额",
                dataIndex: "feeCount",
                width: "150px",
                /*render: (text, record) => {
                    if (record.canSee){
                        return <span>{text && Number(text).toFixed(2)}</span>
                    }
                    return <span>******</span>
                }*/
                render: (text, record) => {
                    return <span>{text && Number(text).toFixed(2)}</span>
                }
            }, {
                title: "币种",
                dataIndex: "currency",
                width: "150px",
            }, {
                title: "供应商",
                width: "150px",
                dataIndex: "carrierId"
            }, {
                title: "",
                dataIndex: ""
            }
        ];

        return (

            <div>
                <Table
                    {...tableProps}
                    columns={columns}
                    dataSource={dataSource}
                    loading={loading}
                />
                <UpdateOrderOutFeeModal/>
            </div>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.ghOrderPayable.orderOutFee,
        order: state.ghOrderPayable.modify.model,
        feeList: state.ghOrderPayable.list.feeList
    };
};

export default connect(mapStateToProps)(OrderOutFeeModal);
